<?PHP

class OP_WidgetsAdmin{
	function controller(){
		if (!wp_verify_nonce($_POST['nonce'], 'op-clipboard_sidebar_method') ) die;
		
		$user = wp_get_current_user();
		$key =  'op_clipboard_sidebar-'.$user->ID;
		call_user_func(array('OP_WidgetsAdmin',$_POST['method']),$key);
	}
	
	function copy($key){
		$data = preg_replace('/\s\s+/', ' ', $_POST['html']);

		preg_match_all('/id=\\\"[a-z0-9-\_]+?-([0-9]+)\\\"/i', $data, $ids); // find all id's
		foreach($ids[1] as $id){
			$new_id =  OP::get_next_widget_id();
			$data = str_replace($id, $new_id, $data);
		}

		$data = preg_replace('/jQuery[0-9]+=\\\"[0-9]+\\\\"/','',$data); // remove jquery def (comes through in IE markup)
		$data = preg_replace('/isDraggable=\\\"true\\\"/','',$data);  // remove jquery sortable's draggable attribute (comes through in IE markup)

		if($output = get_option($key)){
			update_option($key,$data);
		}
		else{
			add_option($key,$data);
		}
		return;
	}
	
	function paste($key){
		if($output = get_option($key)){
			$output = preg_replace('/sidebar=([a-z0-9\-\_]+)\&/i','sidebar='.$_POST['sidebar'].'&',$output);
			die($output);
		}
		die;
	}
	
	function inclipboard($key){
		if($output = get_option($key)) die('TRUE');
		die('FALSE');
	}
}

?>